﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Whois
{
    public partial class frmWhois : Form
    {
        Hashtable htWhoIs = new Hashtable();

        public frmWhois()
        {
            InitializeComponent();

            LoadData();
        }

        private void LoadData()
        {
            string strContents = string.Empty;

            if (!File.Exists("Whois.txt"))
            {
                MessageBox.Show("Whois data file could not be found.");
                Environment.Exit(-1);
            }

            using (StreamReader sr = new StreamReader("Whois.txt"))
            {
                strContents = sr.ReadToEnd();
                sr.Close();
            }

            string[] strServers = strContents.Split(Environment.NewLine.ToCharArray());
            foreach (string strServer in strServers)
            {
                if (strServer.StartsWith(";"))
                    continue;
                else
                {
                    string[] arrDomain = strServer.Split(' ');
                    if (arrDomain.Length == 2)
                    {
                        if (!htWhoIs.Contains(arrDomain[0]))
                            htWhoIs.Add(arrDomain[0],
                                        arrDomain[1]);
                    }
                }
            }
        }

        private void btnWhoIs_Click(object sender, EventArgs e)
        {
            string strWhoIsServer = string.Empty;
            string strDomain = txtDomainName.Text;
            if (txtWhoIsServer.Text.Length < 1)
            {
                string[] arrTLD = strDomain.Split('.');
                string strTLD = arrTLD[arrTLD.Length - 1];

                strWhoIsServer = htWhoIs[strTLD] != null ? htWhoIs[strTLD].ToString() : string.Empty;
            }
            else
            {
                strWhoIsServer = string.Empty;
            }

            if (strWhoIsServer != null)
            {
                string strOutput = GetWhoisInformation(strWhoIsServer, strDomain);
                
                if (strOutput.StartsWith("**ERROR**"))
                {
                    txtOutput.Text = strOutput.TrimStart("**ERROR**".ToCharArray());
                    txtOutput.ForeColor = Color.Red;
                }
                else
                {
                    txtOutput.Text = strOutput;
                    txtOutput.ForeColor = Color.Black;
                }

                if (chkCopyOutputToClipboard.Checked)
                    Clipboard.SetData(DataFormats.Text, txtOutput.Text);
            }
            else
            {
                MessageBox.Show("We do not have a pertinent Whois server for this domain");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private string GetWhoisInformation(string whoisServer, string url)
        {
            try
            {
                StringBuilder stringBuilderResult = new StringBuilder();
                TcpClient tcpClinetWhois = new TcpClient(whoisServer, 43);
                NetworkStream networkStreamWhois = tcpClinetWhois.GetStream();
                BufferedStream bufferedStreamWhois = new BufferedStream(networkStreamWhois);
                StreamWriter streamWriter = new StreamWriter(bufferedStreamWhois);

                streamWriter.WriteLine(url);
                streamWriter.Flush();

                StreamReader streamReaderReceive = new StreamReader(bufferedStreamWhois);

                while (!streamReaderReceive.EndOfStream)
                    stringBuilderResult.AppendLine(streamReaderReceive.ReadLine());

                return stringBuilderResult.ToString();
            }
            catch (Exception SocketException)
            {
                return ("**ERROR**"+SocketException.Message);
            }
        }

        private void gpOutput_Enter(object sender, EventArgs e)
        {

        }
    }
}
